import {
	getCompanyList,
	getMyInfo
} from '@/utils/req/api.js'

export default {
	namespaced: true,
	state() {
		return {
			companyList: [],
			myInfo: ''
		}
	},
	mutations: {
		setCompanyList(state, data) {
			state.companyList = data
		},
		setUserInfo(state, data) {
			state.myInfo = data
		}
	},
	actions: {
		async _getMyInfo({
			state,
			commit,
			dispatch
		}, type) {
			let res = await getMyInfo();
			commit('setUserInfo', res.data.result);
			uni.setStorageSync('myInfo', res.data.result);
			dispatch('_getCompanyList', type, res.data.result.id);
		},
		async _getCompanyList({
			commit,
			state
		}, type, merchantId) {
			let res = await getCompanyList({
				pageSize: 99999,
				pageNo: 1,
				merchantId:uni.getStorageSync('myInfo').id
			});
			res.data.result.forEach(item => {
				item.text = item.companyShortName
			})
			let arr = res.data.result;
			arr=arr.filter(item=>item.isDisplayMerchant==1);
			if (type == 'order' || type == 'datas'||type=='afterOrder') {
				if(type=='order'){
					arr=arr.filter(item=>item.isOpenPrivateOrder!='yes');
				}
				arr = [{
					companyShortName: '个人订单'
				}, ...arr]
			} else if (type == 'food') {
				arr = [{
					companyShortName: '全部商品'
				}, ...arr]
			}
			commit('setCompanyList', arr);
		}
	},
	getters: {},
}
